package com.jzd1997.lesson1;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.Scanner;

import static org.junit.Assert.assertEquals;

/**
 * @author jiangzd
 * @date 2019/7/8 17:34
 * 简单工厂模式
 */
public class TestOperation {
    public static void main(String[] args) {
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.printf("是否继续计算，Y：Yes N:No");
            String input = "";
            input = scanner.nextLine();
            if(StringUtils.equalsIgnoreCase("N",input)){
                break;
            }
            System.out.println("请输入数字1");
            input = scanner.nextLine();
            double operand1 = Double.parseDouble(input);
            System.out.println("请输入运算符");
            String operator = scanner.nextLine();
            System.out.println("请输入数字2");
            input = scanner.nextLine();
            Operation operation = null;
            double operand2 = Double.parseDouble(input);
            switch (operator){
                case "+":
                    operation = new OperationAdd();
                    break;
                case "-":
                    operation = new OperationSub();
                    break;
                case "*":
                    operation = new OperationMul();
                    break;
                case "/":
                    operation = new OperationDiv();
                    break;
            }
            operation.setOperandA(operand1);
            operation.setOperandB(operand2);
            operation.setOperator(operator);
            operation.printResult();
        }
    }

    @Test
    public void testStringEqual(){
        Assert.assertTrue(StringUtils.equals(null,null));
        Assert.assertFalse(StringUtils.equals(null,"null"));
        Assert.assertTrue(StringUtils.equals("null","null"));
    }

    @Test
    public void testToInt(){
        Assert.assertEquals(1,(int)NumberUtils.toDouble("1.3"));
    }

}
